Page({
    data: {
        avatarUrl: '', //上传头像
        region: ['云南省', '昆明市', '五华区'],
        cityCode: [],
        noteData: '',
        btnLoading: false,
        nickText: '', //昵称
        nameText: '', //名字
        phoneText: '', //手机号
    },

    onLoad(options) {
        // 数据回显
        const data = this.data;
        const that = this;
        Object.defineProperty(data, 'userInfo', {
            set(userInfo) {
                console.log('用户信息', userInfo);
                wx.nextTick(() => {
                    that.setData({
                        avatarUrl: (that.data.avatarUrl && that.data.avatarUrl) || userInfo.headimgurl,
                        nickText: (that.data.nickText && that.data.nickText) || userInfo.nickname,
                        phoneText: (that.data.phoneText && that.data.phoneText) || userInfo.tel,
                        nameText: (that.data.nameText && that.data.nameText) || userInfo.name,
                        noteData: (that.data.noteData && that.data.noteData) || userInfo.address_code_name.join('-'),
                        cityCode: userInfo.address_code,
                    });
                });
            },
        });
    },

    bindRegionChange(e) {
        this.setData({
            noteData: e.detail.value.join('-'),
            cityCode: e.detail.code,
        });
    },

    onChooseAvatar(e) {
        console.log(e);
        wx.showLoading({
            title: '上传中',
            mask: true,
        });
        let that = this;
        const { avatarUrl } = e.detail;
        wx.uploadFile({
            url: 'https://zjkj.kmmyxb.net.cn/common/aliYunUpload',
            filePath: avatarUrl,
            name: 'file',
            header: {
                Authorization: wx.getStorageSync('Authorization'),
            },
            success(res) {
                console.log(res);
                const data = JSON.parse(res.data).data;
                console.log(data);
                that.setData({
                    avatarUrl: data,
                });
            },
            fail() {
                wx.showToast({
                    title: '上传失败，请重试',
                    icon: 'none',
                });
            },
            complete() {
                wx.hideLoading();
            },
        });
    },

    onNameInput(e) {
        this.setData({
            nameText: e.detail.value,
        });
},

    onPhoneInput(e) {
        this.setData({
            phoneText: e.detail.value,
        });
    },
    onNickInput(e) {
        console.log(e);
        this.setData({
            nickText: e.detail.value,
        });
    },

    submit() {
        const { avatarUrl, region, cityCode, noteData, btnLoading, nickText, nameText, phoneText } = this.data;

        if (!nickText)
            return wx.showToast({
                title: '请填写昵称',
                icon: 'none',
            });

        if (!nameText)
            return wx.showToast({
                title: '请填写名字',
                icon: 'none',
            });

        if (!phoneText)
            return wx.showToast({
                title: '请输入手机号',
                icon: 'none',
            });
        if (phoneText.length != 11)
            return wx.showToast({
                title: '请输入正确手机号',
                icon: 'none',
            });
        if (noteData == '')
            return wx.showToast({
                title: '请选择地址',
                icon: 'none',
            });

        this.setData({
            btnLoading: true,
        });

        this.request({
            url: 'user/perfectUserInfo',
            method: 'POST',
            data: {
                nickname: nickText,
                headimgurl: avatarUrl,
                name: nameText,
                tel: phoneText,
                address_code: cityCode,
            },
        })
            .then(async res => {
                wx.showToast({
                    title: res.msg,
                });
                this.setData({
                    btnLoading: false,
                });
                await this.sleep(1200);
                wx.switchTab({
                    url: '/pages/user/user',
                });
            })
            .catch(err => {
                this.setData({
                    btnLoading: false,
                });
            });
    },
});
